package com.axxc.jpa.annotation;

import java.lang.reflect.Method;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;
import org.springframework.util.ReflectionUtils;

/**
 * @ClassName: SelfDefinitionAnnotationHandlerBean
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author xxc
 * @date 2018年9月17日 下午5:55:45
 * @see {@link https://gitee.com/biuabiu }
 */
@Component
public class SelfDefinitionAnnotationHandlerBean implements BeanPostProcessor {

	// bean实例前,实现扫描注解?只有一次性啊,并不是动态执行
	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		Method[] methods = ReflectionUtils.getAllDeclaredMethods(bean.getClass());
		if (methods != null) {
			for (Method method : methods) {
				SaveOrUpdate saveOrUpdate = AnnotationUtils.findAnnotation(method, SaveOrUpdate.class);
				// process
				if (saveOrUpdate != null) {
					System.out.println(method.getName());
					System.out.println(saveOrUpdate.value());
				}
			}
		}
		return bean;
	}

	// bean实例后
	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		System.err.println("已完成实例化");
		System.out.println(beanName);
		return bean;
	}

}
